package com.imooc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imooc.mapper.*;
import com.imooc.pojo.*;
import com.imooc.pojo.vo.CommentLevelCountsVO;
import com.imooc.pojo.vo.ItemCommentVO;
import com.imooc.pojo.vo.SearchItemsVO;
import com.imooc.pojo.vo.ShopcartVO;
import com.imooc.service.ItemService;
import imooc.enums.CommentLevel;
import imooc.enums.YesOrNo;
import imooc.utils.DesensitizationUtil;
import imooc.utils.PagedGridResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @author cg.hu
 * @version 1.0.0
 * @date 2021/1/25 16:59
 * @description
 * @since 1.0.0
 */
@Service
public class ItemServiceImpl implements ItemService {

    final static Logger logger = LoggerFactory.getLogger(ItemServiceImpl.class);

    @Autowired
    private ItemsMapper itemsMapper;

    @Autowired
    private ItemsImgMapper itemsImgMapper;

    @Autowired
    private ItemsParamMapper itemsParamMapper;

    @Autowired
    private ItemsSpecMapper itemsSpecMapper;

    @Autowired
    private ItemsCommentsMapper itemsCommentsMapper;

    @Autowired
    private ItemsMapperCustom itemsMapperCustom;

    /**
     * @author cg.hu
     * @version 2.6
     * @date 2021/1/25 17:13
     * @description 根据商品ID，查询详情
     **/
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    public Items queryItemById(String itemId)  throws Exception{
        return itemsMapper.selectByPrimaryKey(itemId);
    }

    /**
     * @author cg.hu
     * @version 2.6
     * @date 2021/1/25 17:13
     * @description 根据商品ID，查询图片列表
     **/
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    public List<ItemsImg> queryItemImgList(String itemId)  throws Exception{
        Example example = new Example(ItemsImg.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("itemId", itemId);
        return itemsImgMapper.selectByExample(example);
    }

    /**
     * @author cg.hu
     * @version 2.6
     * @date 2021/1/26 8:37
     * @description 根据商品ID，查询商品规格列表
     **/
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    public List<ItemsSpec> queryItemSpecList(String itemId)  throws Exception{
        Example example = new Example(ItemsSpec.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("itemId", itemId);
        return itemsSpecMapper.selectByExample(example);
    }

    /**
     * @author cg.hu
     * @version 2.6
     * @date 2021/1/26 8:37
     * @description 根据商品ID，查询商品参数
     **/
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    public ItemsParam queryItemParam(String itemId)  throws Exception{
        Example example = new Example(ItemsParam.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("itemId", itemId);
        return itemsParamMapper.selectOneByExample(example);
    }

    /**
     * @author cg.hu
     * @version 2.6
     * @date 2021/1/26 15:37
     * @description 根据商品ID,查询商品评价等级数量
     **/
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    public CommentLevelCountsVO queryCommentCounts(String itemId) throws Exception {

        Integer goodCounts = getCommentCounts(itemId, CommentLevel.GOOD.type);
        Integer normalCounts = getCommentCounts(itemId, CommentLevel.NORMAL.type);
        Integer badCounts = getCommentCounts(itemId, CommentLevel.BAD.type);
        Integer totalCounts = goodCounts + normalCounts + badCounts;

        CommentLevelCountsVO commentLevelCountsVO = new CommentLevelCountsVO();
        commentLevelCountsVO.setBadCounts(badCounts);
        commentLevelCountsVO.setGoodCounts(goodCounts);
        commentLevelCountsVO.setNormalCounts(normalCounts);
        commentLevelCountsVO.setTotalCounts(totalCounts);
        return commentLevelCountsVO;
    }

    /**
     * @author cg.hu
     * @version 2.6
     * @date 2021/1/26 15:51
     * @description    获取商品评论
     **/
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    public Integer getCommentCounts(String itemId, Integer level) {
        ItemsComments condition = new ItemsComments();
        condition.setItemId(itemId);
        if (null != level) {
            condition.setCommentLevel(level);
        }
        return itemsCommentsMapper.selectCount(condition);
    }

    /**
     * @author cg.hu
     * @version 2.6
     * @date 2021/1/27 9:06
     * @description    分页查询评论信息
     **/
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    public PagedGridResult queryPagedComment(String itemId,
                                                 Integer level,
                                                 Integer page,
                                                 Integer pageSize) throws Exception {
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("itemId", itemId);
        paramsMap.put("level", level);

        PageHelper.startPage(page, pageSize);
        List<ItemCommentVO> itemCommentVOList = itemsMapperCustom.queryItemComments(paramsMap);
        // 脱敏
        for (ItemCommentVO itemCommentVO : itemCommentVOList) {
            itemCommentVO.setNickname(DesensitizationUtil.commonDisplay(itemCommentVO.getNickname()));
        }
        PagedGridResult pagedGridResult = setterPagedGrid(itemCommentVOList, page);

        return pagedGridResult;
    }

    /**
     * @author cg.hu
     * @version 2.6
     * @date 2021/1/27 10:20
     * @description  组装 PagedGridResult
     **/
    private PagedGridResult setterPagedGrid(List<?> list, Integer page) {
        PageInfo<?> pageInfo = new PageInfo<>(list);
        PagedGridResult pagedGridResult = new PagedGridResult();
        pagedGridResult.setPage(page);
        pagedGridResult.setRows(list);
        pagedGridResult.setTotal(pageInfo.getPages());
        pagedGridResult.setRecords(pageInfo.getTotal());
        return pagedGridResult;
    }

    /**
     * @author cg.hu
     * @version 2.6
     * @date 2021/1/27 14:32
     * @description    搜索商品 分页
     **/
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    public PagedGridResult searchItems(String keywords, String sort, Integer page, Integer pageSize) throws Exception {
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("keywords", keywords);
        paramsMap.put("sort", sort);

        PageHelper.startPage(page, pageSize);
        List<SearchItemsVO> searchItemsVOList = itemsMapperCustom.searchItems(paramsMap);
        PagedGridResult pagedGridResult = setterPagedGrid(searchItemsVOList, page);
        return pagedGridResult;
    }

    /**
     * @author cg.hu
     * @version 2.6
     * @date 2021/1/28 8:50
     * @description    根据商品分类搜索商品
     **/
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    public PagedGridResult searchItemsByThirdCat(String catId, String sort, Integer page, Integer pageSize) throws Exception {
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("catId", catId);
        paramsMap.put("sort", sort);

        PageHelper.startPage(page, pageSize);
        List<SearchItemsVO> searchItemsVOList = itemsMapperCustom.searchItemsByThirdCat(paramsMap);
        PagedGridResult pagedGridResult = setterPagedGrid(searchItemsVOList, page);
        return pagedGridResult;
    }

    /**
     * @author cg.hu
     * @version 2.6
     * @date 2021/1/28 11:27
     * @description    根据规格 ids 查询最新的购物车中商品数据
     **/
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    public List<ShopcartVO> queryItemsBySpecIds(String specIds) throws Exception {
        String[] specIdsArr = specIds.split(",");
        List<String> specIdsList = new ArrayList<>();
        Collections.addAll(specIdsList, specIdsArr);
        List<ShopcartVO>  shopcartVOList = itemsMapperCustom.queryItemsBySpecIds(specIdsList);
        return shopcartVOList;
    }

    /**
     * @author cg.hu
     * @version 2.6
     * @date 2021/2/16 20:20
     * @description    根据规格商品规格ID，获取商品规格对象
     **/
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    public ItemsSpec queryItemSpecById(String specId) throws Exception {
        return itemsSpecMapper.selectByPrimaryKey(specId);
    }

    /**
     * @author cg.hu
     * @version 2.6
     * @date 2021/2/16 20:20
     * @description    根据商品ID，获取商品主图
     **/
    @Override
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    public String queryItemMainImgById(String itemId) throws Exception {
        ItemsImg itemsImg = new ItemsImg();
        itemsImg.setIsMain(YesOrNo.YES.type);
        itemsImg.setItemId(itemId);
        ItemsImg result = itemsImgMapper.selectOne(itemsImg);
        return result != null ? result.getUrl() : "";
    }
}
